package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.QrCodeInfoMapper;
import cc.rengu.igas.bomp.common.entity.QrCodeInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/2
 */
public class QrCodeInfoMapperImpl implements QrCodeInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertQrCodeInfo(QrCodeInfo qrCodeInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertQrCodeInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, qrCodeInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记二维码信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", qrCodeInfo.getInstId(), qrCodeInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int insertQrCodeInfoByTransaction(QrCodeInfo qrCodeInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertQrCodeInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, qrCodeInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记二维码信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", qrCodeInfo.getInstId(), qrCodeInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateQrCodeInfoById(QrCodeInfo qrCodeInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateQrCodeInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, qrCodeInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记二维码信息失败,instId:<{}>,mchntNo:<{}>,id:<{}>,iReturnCode:<{}>", qrCodeInfo.getInstId(), qrCodeInfo.getMchntNo(), qrCodeInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public QrCodeInfo selectQrCodeInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectQrCodeInfoById");
        QrCodeInfo qrCodeInfo = new QrCodeInfo();
        qrCodeInfo.setInstId(instId);
        qrCodeInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, qrCodeInfo, qrCodeInfo);
        if (0 != iReturnCode) {
            rglog.error("获取二维码信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return qrCodeInfo;
    }

    @Override
    public QrCodeInfo selectQrCodeInfoByPrimaryKey(String instId, String qrUrl) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectQrCodeInfoByPrimaryKey");
        QrCodeInfo qrCodeInfo = new QrCodeInfo();
        qrCodeInfo.setInstId(instId);
        qrCodeInfo.setQrUrl(qrUrl);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, qrCodeInfo, qrCodeInfo);
        if (0 != iReturnCode) {
            rglog.error("获取二维码信息失败,instId:<{}>,qrUrl:<{}>,iReturnCode:<{}>", instId, qrUrl, iReturnCode);
            return null;
        }
        return qrCodeInfo;
    }

    @Override
    public QrCodeInfo selectQrCodeInfoByQrCode(String instId, String qrCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectQrCodeInfoByQrCode");
        QrCodeInfo qrCodeInfo = new QrCodeInfo();
        qrCodeInfo.setInstId(instId);
        qrCodeInfo.setQrCode(qrCode);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, qrCodeInfo, qrCodeInfo);
        if (0 != iReturnCode) {
            rglog.error("获取二维码信息失败,instId:<{}>,qrCode:<{}>,iReturnCode:<{}>", instId, qrCode, iReturnCode);
            return null;
        }
        return qrCodeInfo;
    }

    @Override
    public QrCodeInfo selectQrCodeInfoByUserId(String instId, String userId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectQrCodeInfoByUserId");
        QrCodeInfo qrCodeInfo = new QrCodeInfo();
        qrCodeInfo.setInstId(instId);
        qrCodeInfo.setUserId(userId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, qrCodeInfo, qrCodeInfo);
        if (0 != iReturnCode) {
            rglog.error("获取二维码信息失败,instId:<{}>,userId:<{}>,iReturnCode:<{}>", instId, userId, iReturnCode);
            return null;
        }
        return qrCodeInfo;
    }

    @Override
    public List<QrCodeInfo> selectQrCodeInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectQrCodeInfoByMchntNo");
        QrCodeInfo qrCodeInfo = new QrCodeInfo();
        qrCodeInfo.setInstId(instId);
        qrCodeInfo.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, qrCodeInfo, QrCodeInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取二维码信息失败,instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (QrCodeInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<QrCodeInfo> selectQrCodeInfoByChainMchntNo(String instId, String chainMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectQrCodeInfoByChainMchntNo");
        QrCodeInfo qrCodeInfo = new QrCodeInfo();
        qrCodeInfo.setInstId(instId);
        qrCodeInfo.setChainMchntNo(chainMchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, qrCodeInfo, QrCodeInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取二维码信息失败,instId:<{}>,chainMchntNo:<{}>", instId, chainMchntNo);
            return null;
        }
        return objectList.stream().map(item -> (QrCodeInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<QrCodeInfo> selectQrCodeInfoByApplyBatchNo(String instId, String applyBatchNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectQrCodeInfoByApplyBatchNo");
        QrCodeInfo qrCodeInfo = new QrCodeInfo();
        qrCodeInfo.setInstId(instId);
        qrCodeInfo.setApplyBatchNo(applyBatchNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, qrCodeInfo, QrCodeInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取二维码信息失败,instId:<{}>,applyBatchNo:<{}>", instId, applyBatchNo);
            return null;
        }
        return objectList.stream().map(item -> (QrCodeInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectQrCodeInfoByApplyBatchNo(String instId, String applyBatchNo, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectQrCodeInfoByApplyBatchNo");
        QrCodeInfo qrCodeInfo = new QrCodeInfo();
        qrCodeInfo.setInstId(instId);
        qrCodeInfo.setApplyBatchNo(applyBatchNo);

        return dbsUtil.dbsFetchPageListData(sqlStr, qrCodeInfo, QrCodeInfo.class, pageInfo);
    }

    @Override
    public QrCodeInfo selectQrCodeInfoNum(String instId, String mchntNo, String status) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectQrCodeInfoNum");
        QrCodeInfo qrCodeInfo = new QrCodeInfo();
        qrCodeInfo.setInstId(instId);
        qrCodeInfo.setMchntNo(mchntNo);
        qrCodeInfo.setQrStatus(status);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, qrCodeInfo, qrCodeInfo);
        if (0 != iReturnCode) {
            rglog.error("获取二维码信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, mchntNo, iReturnCode);
            return null;
        }
        return qrCodeInfo;
    }

    @Override
    public int updateQrCodeInfoStatusByMchntNo(QrCodeInfo qrCodeInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateQrCodeInfoStatusByMchntNo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, qrCodeInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新二维码状态信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", qrCodeInfo.getInstId(), qrCodeInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

}
